﻿using Microsoft.Xna.Framework;
using RaptorEngine;
using RaptorEngine.Components;
using RaptorEngine.Components.Drawable;

namespace OilRigSumoWrestlers.Controllables
{
    class CountdownTimer:Controllable
    {
        private DrawableText _text;
        private int _milliseconds;
        private int _millisecondsPassed=0;

        private string[] _countdownText = new string[]
            {
                "Get Ready", "Set", "Fight!",""
            };

        public CountdownTimer(Entity entity, DrawableText text, int seconds) : base(entity)
        {
            _milliseconds = seconds*1000;
            _text = text;
        }

        public override void Update(GameTime gameTime)
        {
            _millisecondsPassed += gameTime.ElapsedGameTime.Milliseconds;


            if (_text.TextString != _countdownText[(int) (_millisecondsPassed/1000)])
            {
                _text.TextString = _countdownText[(int) (_millisecondsPassed/1000)];

                _text.Entity.Position =
                    new Vector3(
                        GameEngine.Instance.Render.GraphicsDevice.Viewport.TitleSafeArea.X +
                        GameEngine.Instance.Render.GraphicsDevice.Viewport.TitleSafeArea.Width/2,
                        GameEngine.Instance.Render.GraphicsDevice.Viewport.TitleSafeArea.Y +
                        GameEngine.Instance.Render.GraphicsDevice.Viewport.TitleSafeArea.Height/2, 0);
                _text.CenterText();
            }

            if (_millisecondsPassed >= _milliseconds)
            {
                TimeExpired();
            }
        }

        private void TimeExpired()
        {
            GameEngine.Instance.Scene.RemoveCurrentScene();
            GameEngine.Instance.Scene.ShowCurrentScene();
            GameEngine.Instance.Scene.StartCurrentScene();
        }
    }
}
